﻿using System;

namespace Results.Models
{
    /// <summary>
    /// The results (scores) of a specific player at a specific tournament
    /// </summary>
    public class ResultModel : IComparable
    {
        /// <summary>
        /// Data
        /// </summary>
        public String Place { get; set; }
        public String LastName { get; set; }
        public String FirstName { get; set; }
        public String Suffix { get; set; }
        public String Hometown { get; set; }
        public String GraduationYear { get; set; }
        public String Round1 { get; set; }
        public String Round2 { get; set; }
        public String Total { get; set; }
        public String Points { get; set; }
        public DateTime Date { get; set; }
        public String Group { get; set; }

        /// <summary>
        /// The tournament that the results are for
        /// </summary>
        public TournamentModel TournamentModel { get; set; }

        /// <summary>
        /// The player that the results are for
        /// </summary>
        public PlayerModel PlayerModel { get; set; }

        public ResultModel()
        {
            TournamentModel = new TournamentModel();
            PlayerModel = new PlayerModel();
        }

        public int PointValue
        {
            get
            {
                try
                {
                    return Convert.ToInt32(Points);
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        #region IComparable Members

        /// <summary>
        /// Sort by date, then group, then player
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            var otherResultModel = obj as ResultModel;

            if (otherResultModel == null)
            {
                return 0;
            }

            int retVal = Date.CompareTo(otherResultModel.Date) * -1;

            if (retVal == 0)
            {
                retVal = Group.CompareTo(otherResultModel.Group);
            }

            if (retVal == 0)
            {
                retVal = PlayerModel.CompareTo(otherResultModel.PlayerModel);
            }

            return retVal;
        }

        #endregion
    }
}
